import * as path from "path";
import { HttpServer, WsServer } from "tsrpc";
import { disconnectCluster, updateClusterInfo } from "./gameServerMgr";
import { startWatchGateConfig } from "./gateConfigMgr";
import { serviceProto as clientServiceProto } from "./shared/gateClient/protocols/serviceProto";
import { serviceProto as clusterServiceProto } from "./shared/gameCluster/protocols/serviceProto";
import { initRedisClient } from "./shared/gameCluster/redisHelper";
import { logger } from "./shared/tsgf/logger";

const gateServer = new HttpServer(clientServiceProto, {
    port: 7100,
    json: false,
    logger:logger,
});
const clusterServer = new WsServer(clusterServiceProto, {
    port: 7101,
    json: false,
    logger:logger,
});

async function initServer() {
    await gateServer.autoImplementApi(path.resolve(__dirname, 'api/gateClient'));
    await clusterServer.autoImplementApi(path.resolve(__dirname, 'api/gameCluster'));
    clusterServer.flows.postDisconnectFlow.push((v) => {
        disconnectCluster(v.conn);
        return v;
    });
    clusterServer.listenMsg("SyncServerInfo", call => {
        updateClusterInfo(call.conn, call.msg.serverInfo);
    });
};

async function main() {
    var gateCfg = startWatchGateConfig();
    initRedisClient(gateCfg.authRedisConfig);

    await initServer();
    await gateServer.start();
    gateServer.logger?.log("gate服务启动成功!");
    await clusterServer.start();
    clusterServer.logger?.log("集群服务启动成功!");

};

main();

